package com.micro.ai.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.auth.entity.DictType;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 字典类型Mapper接口
 *
 * @author micro-ai
 * @since 2025-01-29
 */
@Mapper
public interface DictTypeMapper extends BaseMapper<DictType> {

    /**
     * 分页查询字典类型
     *
     * @param page 分页参数
     * @param code 字典类型编码
     * @param name 字典类型名称
     * @param status 状态
     * @param tenantId 租户ID
     * @return 分页结果
     */
    @Select("<script>" +
            "SELECT dt.*, t.name as tenant_name " +
            "FROM dict_types dt " +
            "LEFT JOIN tenants t ON dt.tenant_id = t.id " +
            "WHERE dt.deleted_at IS NULL " +
            "<if test='code != null and code != \"\"'>" +
            "AND dt.code LIKE CONCAT('%', #{code}, '%') " +
            "</if>" +
            "<if test='name != null and name != \"\"'>" +
            "AND dt.name LIKE CONCAT('%', #{name}, '%') " +
            "</if>" +
            "<if test='status != null and status != \"\"'>" +
            "AND dt.status = #{status} " +
            "</if>" +
            "<if test='tenantId != null and tenantId != \"\"'>" +
            "AND dt.tenant_id = #{tenantId} " +
            "</if>" +
            "ORDER BY dt.sort_order ASC, dt.created_at DESC" +
            "</script>")
    IPage<DictType> selectDictTypePage(Page<DictType> page,
                                       @Param("code") String code,
                                       @Param("name") String name,
                                       @Param("status") String status,
                                       @Param("tenantId") String tenantId);

    /**
     * 根据编码查询字典类型
     *
     * @param code 字典类型编码
     * @param tenantId 租户ID
     * @return 字典类型
     */
    @Select("SELECT * FROM dict_types WHERE code = #{code} AND deleted_at IS NULL " +
            "AND (tenant_id = #{tenantId} OR tenant_id IS NULL) " +
            "ORDER BY tenant_id DESC LIMIT 1")
    DictType selectByCode(@Param("code") String code, @Param("tenantId") String tenantId);

    /**
     * 查询所有启用的字典类型
     *
     * @param tenantId 租户ID
     * @return 字典类型列表
     */
    @Select("SELECT * FROM dict_types WHERE status = 'active' AND deleted_at IS NULL " +
            "AND (tenant_id = #{tenantId} OR tenant_id IS NULL) " +
            "ORDER BY sort_order ASC, created_at ASC")
    List<DictType> selectActiveDictTypes(@Param("tenantId") String tenantId);

    /**
     * 根据ID直接查询字典类型（不应用逻辑删除过滤，用于物理删除前检查）
     *
     * @param id 字典类型ID
     * @return 字典类型
     */
    @Select("SELECT * FROM dict_types WHERE id = #{id}")
    DictType selectByIdIgnoreLogic(@Param("id") String id);

    /**
     * 物理删除字典类型（直接执行 DELETE SQL，不应用逻辑删除）
     *
     * @param id 字典类型ID
     * @return 删除的行数
     */
    @Delete("DELETE FROM dict_types WHERE id = #{id}")
    int deleteByIdPhysical(@Param("id") String id);
}
